package com.jerry.timer;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.jerry.scan.utils.ScanIP;
import com.jerry.utils.str.Jutils;
import com.jerry.utils.zbx.ZBX;

@Configuration
@Component
@EnableAsync
public class JmxDis {
	
	private static final Logger log = LoggerFactory.getLogger(JmxDis.class);
    @Async
	@Scheduled (fixedDelay = 864000) //"0/5 * * * * ?"  ${timer2.cron}
	public void timer1() {

		log.warn("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
	        // this.getHost();
	 		log.warn("a" + Jutils.getProperty("scan.miniport"));
	         //Thread.sleep(1000 * 10);
	 		List<String> hostList=new ArrayList<>();
	 		if(!Jutils.getProperty("host.listEnable").trim().equals("1")) {
		         JSONObject hostJsonObject=ZBX.getHost();
		         hostList = ZBX.getHostList(hostJsonObject);
		         log.warn("hostList1 size is : "+hostList.size());
		         
	 		}else {
	 			String hostStr=Jutils.getProperty("host.list").trim();
	 			String[] arrHost=hostStr.split("-");
	 			log.warn(arrHost[0]+" and "+arrHost[1]);
	 			int beginIP=Integer.parseInt(arrHost[0].split("\\.")[3]);
	 			int endIP=Integer.parseInt(arrHost[1]);
	 			String subIP=arrHost[0].split("\\.")[0]+"."+arrHost[0].split("\\.")[1]+"."+arrHost[0].split("\\.")[2]+".";
	 			for (int i=beginIP;i<=endIP;i++) {
	 				hostList.add(subIP+i);
	 			}
	 		}
	 		log.warn("HostList size is "+hostList.size());
	 		for (int i=0;i<hostList.size();i++ ) {
		         
	       	 try {
				ScanIP.doScan(hostList.get(i));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       
	        }

	         
	     }
	
}
